<template>
  <div>
    <el-button
      type="primary"
      @click="handleAdd"
      class="mb20"
      icon="el-icon-circle-plus-outline"
    >未签订原因</el-button>
    <notsigningDialog
      :title="title"
      :addDialog="addDialog"
      :editDialog="editDialog"
      :notsigningForm="notsigningForm"
      @close="hanleClose"
      @update="handleUpdate"
    ></notsigningDialog>
    <el-table
      v-loading="loading"
      element-loading-text="拼命加载中"
      border
      :data="tableData.filter(item => !search || item.name.toLowerCase().includes(search.toLowerCase()))"
      style="width: 100%"
    >
      <el-table-column type="selection" width="65"></el-table-column>
      <el-table-column prop="id" label="序号" width="65"></el-table-column>
      <el-table-column sortable prop="createdAt" label="创建时间" width="180">
        <template slot-scope="scope">{{scope.row.createdAt | moment("YYYY/MM/DD HH:mm:ss")}}</template>
      </el-table-column>
      <el-table-column prop="name" label="未签订原因" width="180"></el-table-column>
      <el-table-column prop="operation" label="操作" width="320">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            @click="handleEdit(scope.row)"
            icon="el-icon-edit-outline"
          >编辑</el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.row.id)"
            icon="el-icon-delete"
          >删除</el-button>
        </template>
      </el-table-column>
      <el-table-column prop="search" label="搜索">
        <template slot="header" slot-scope="scope">
          <el-input v-model="search" size="mini" placeholder="输入未签订原因搜索"/>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>
<script>
import notsigningDialog from "@/components/notsigning_dialog";

export default {
  name: "reasons_not_signing",
  data() {
    return {
      search: "",
      title: "",
      tableData: [],
      addDialog: false,
      loading: true,
      editDialog: false,
      notsigningForm: {
        name: ""
      }
    };
  },
  created() {
    this.getNotsignings();
  },
  methods: {
    getNotsignings() {
      this.$axios.get("/api/system/notsignings").then(data => {
        this.tableData = data;
        this.loading = false;
      });
    },
    handleAdd() {
      this.addDialog = !this.addDialog;
      this.title = "添加未签订原因";
    },
    handleEdit(item) {
      this.notsigningForm = item;
      this.editDialog = !this.editDialog;
      this.title = "编辑未签订原因";
    },
    handleDelete(id) {
      this.$confirm("此操作将删除该条未签订原因, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.$axios
            .delete("/api/system/removeNotsigning/" + id)
            .then(data => {
              const { status } = data;
              if (!status) {
                this.$message({
                  type: "warning",
                  message: data.msg
                });
                return;
              }
              this.$message({
                type: "success",
                message: "删除成功!"
              });
              this.getNotsignings();
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    hanleClose() {
      this.addDialog = false;
      this.editDialog = false;
      this.resetForm();
    },
    handleUpdate() {
      this.getNotsignings();
    },
    resetForm() {
      this.notsigningForm = {};
    }
  },
  components: {
    notsigningDialog
  }
};
</script>
<style>
</style>
